package sesi.ejava.control;

import sesi.ejava.bcam.R;
import sesi.ejava.settings.BaseSettings;
import sesi.ejava.stream.ControlOutputStream;
import sesi.ejava.stream.ControlOutputStream.ControlData;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

public class ComfortActivity extends Activity {
	
	private ToggleButton buttonFLWindow ;
	private ToggleButton buttonFRWindow ;
	private ToggleButton buttonRLWindow ;
	private ToggleButton buttonRRWindow ;
	
	private ToggleButton buttonLockDoors;
	
	private ToggleButton cockpitLight   ;
	private ToggleButton dashBoardLight ;
	
	private boolean      flWindow       ; // true = open
	private boolean      frWindow		;
	private boolean      rlWindow		;
	private boolean      rrWindow		;
	private boolean		 doorsLock		; // true = locked
	private boolean		 cockpit		; // true = allumé
	private boolean		 dashBoard		; // true = allumé
	
	private ControlOutputStream             stream   ;
	private ControlOutputStream.ControlData sendData ;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.comfort_layout);
		
		int nbWindows = 4 ;
		
		buttonFLWindow = (ToggleButton)findViewById(R.id.toggle_fl_window);
		buttonFRWindow = (ToggleButton)findViewById(R.id.toggle_fr_window);
		buttonRLWindow = (ToggleButton)findViewById(R.id.toggle_rl_window);
		buttonRRWindow = (ToggleButton)findViewById(R.id.toggle_rr_window);
		buttonLockDoors= (ToggleButton)findViewById(R.id.toggle_lock_doors);
		cockpitLight   = (ToggleButton)findViewById(R.id.toggle_cockpit_light);
		dashBoardLight = (ToggleButton)findViewById(R.id.toggle_dashboard_light);
		
        flWindow  = false ;
        frWindow  = false ;
        rlWindow  = false ;
        rrWindow  = false ;
        doorsLock = false ;
        cockpit	  = false ;
        dashBoard = true  ;
        
        dashBoardLight.setChecked(dashBoard) ;

		// récupere l'instance su flux de  sortie
		stream = ControlOutputStream.getInstance() ;
		// Donnée à envoyer		
		//sendData = stream.new ControlData() ;
		sendData = ControlDataSingleton.getInstance().getControlData() ;
		
		nbWindows = Integer.parseInt(BaseSettings.getsettingsSingleton(this).getNbVitres()) ;
		if (nbWindows <= 2) {
			LinearLayout layoutRR = (LinearLayout) findViewById(R.id.linearLayoutWindRR);
			LinearLayout layoutRL = (LinearLayout) findViewById(R.id.linearLayoutWindRL);
			layoutRR.setVisibility(View.GONE);
			layoutRL.setVisibility(View.GONE);
		}
		
		buttonFLWindow.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onFLWindowClicked();
			}
		});
		buttonFRWindow.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onFRWindowClicked();
			}
		});
		buttonRLWindow.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onRLWindowClicked();
			}
		});
		buttonRRWindow.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onRRWindowClicked();
			}
		});
		buttonLockDoors.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onLockClicked();
			}
		});
		cockpitLight.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onCockpitClicked();
			}
		});
		dashBoardLight.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onDashboardClicked();
			}
		});
	}
	
	private void onFRWindowClicked() {
		if (frWindow == false){
			frWindow = true ;
		} else {
			frWindow = false ;
		}
		buttonFRWindow.setChecked(frWindow);
		// set et envoi donnée
		sendCommand(sendData) ;
	}
	private void onFLWindowClicked() {
		if (flWindow == false) {
			flWindow = true ;
		} else {
			flWindow = false ;
		}
		buttonFLWindow.setChecked(flWindow) ;
		// set et envoi donnée	
		sendCommand(sendData);
	}
	private void onRLWindowClicked() {
		if (rlWindow == false) {
			rlWindow = true ;
		} else {
			rlWindow = false ;
		}
		buttonRLWindow.setChecked(rlWindow) ;
		// set et envoi donnée
		sendCommand(sendData);
	}
	private void onRRWindowClicked() {
		if (rrWindow == false) {
			rrWindow = true ;
		} else {
			rrWindow = false ;
		}
		buttonRRWindow.setChecked(rrWindow) ;
		// set et envoi donnée
		sendCommand(sendData);
	}
	private void onLockClicked () {
		if (doorsLock == false) {
			doorsLock = true ;
		} else {
			doorsLock = false ;
		}
		buttonLockDoors.setChecked(doorsLock);
		// set et envoi donnée
		sendCommand(sendData);
	}
    private void onCockpitClicked(){
    	if (cockpit == false) {
    		cockpit = true ;
    	} else {
    		cockpit = false ;
    	}
    	cockpitLight.setChecked(cockpit);
		// set et envoi donnée
    	sendCommand(sendData);
    }
    private void onDashboardClicked(){
    	if (dashBoard == false) {
    		dashBoard = true ;
    	} else {
    		dashBoard = false ;
    	}
    	dashBoardLight.setChecked(dashBoard) ;
		// set et envoi donnée
    	sendCommand(sendData);
    }
    private void sendCommand (ControlData data) {
    	data.setFrontLeftWindow (flWindow );	// flWindow  
        data.setFrontRightWindow(frWindow );	// frWindow
        data.setRearLeftWindow  (rlWindow );	// rlWindow
        data.setRearRightWindow (rrWindow );	// rrWindow
        data.setCarLock			(doorsLock);	// doorsLock
        data.setCockpitLight    (cockpit  );	// cockpit
        data.setDashboardLight  (dashBoard);	// dashBoard
		stream.sendData(data);
	}
    
    protected void onStart() {
		super.onStart();
		
		flWindow  = sendData.isFrontLeftWindow ();
		frWindow  = sendData.isFrontRightWindow();
		rlWindow  = sendData.isRearLeftWindow  ();
		rrWindow  = sendData.isRearRightWindow ();
		doorsLock = sendData.isCarLock         ();
		cockpit   = sendData.isCockpitLight    ();
		dashBoard = sendData.isDashboardLight  ();
		
		buttonFLWindow.setChecked (flWindow );
		buttonFRWindow.setChecked (frWindow );
		buttonRLWindow.setChecked (rlWindow );
		buttonRRWindow.setChecked (rrWindow );
		buttonLockDoors.setChecked(doorsLock);
		cockpitLight.setChecked   (cockpit  );
		dashBoardLight.setChecked (dashBoard);
    }
}
